/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



package sonia.golf.dao.sqlite;

//~--- non-JDK imports --------------------------------------------------------

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import sonia.golf.Course;
import sonia.golf.Hole;
import sonia.golf.dao.CourseDAO;
import sonia.golf.dao.DAOListener.DAOEvent;
import sonia.golf.util.AssertUtil;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Sebastian Sdorra
 */
public class SQLiteCourseDAO extends AbstractSQLiteDAO<Course, String>
        implements CourseDAO
{

  /** Field description */
  public static final String QUERY_HOLE_SELECT_ALL = "hole.select-all";

  /** Field description */
  public static final String QUERY_INSERT = "course.insert";

  /** Field description */
  public static final String QUERY_REMOVE = "course.remove";

  /** Field description */
  public static final String QUERY_SELECT = "course.select";

  /** Field description */
  public static final String QUERY_SELECT_ALL = "course.select-all";

  //~--- constructors ---------------------------------------------------------

  /**
   * Constructs ...
   *
   *
   * @param helper
   */
  SQLiteCourseDAO(SQLiteHelper helper)
  {
    this.helper = helper;
  }

  //~--- methods --------------------------------------------------------------

  /**
   * Method description
   *
   *
   * @param item
   */
  public void add(Course item)
  {
    SQLiteDatabase db = helper.getWritableDatabase();

    db.beginTransaction();

    try
    {
      String query = helper.getQuery(QUERY_INSERT);

      AssertUtil.assertIsNotEmpty(query);

      SQLiteStatement statement = db.compileStatement(query);

      statement.bindString(1, item.getName());
      statement.bindLong(2, item.getCreationDate().getTime());
      statement.executeInsert();

      // TODO add holes ??

      db.setTransactionSuccessful();
      fireEvent(item, DAOEvent.ADD);
    }
    finally
    {
      db.endTransaction();
    }
  }

  /**
   * Method description
   *
   *
   * @return
   */
  public long count()
  {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  /**
   * Method description
   *
   *
   * @param course
   */
  public void fetchHoles(Course course)
  {
    List<Hole> holes = new ArrayList<Hole>();
    Cursor c = helper.executeQuery(QUERY_HOLE_SELECT_ALL);

    try
    {
      while (c.moveToNext())
      {
        holes.add(new Hole(c.getShort(0), c.getShort(1)));
      }
    }
    finally
    {
      c.close();
    }

    course.setHoles(holes);
  }

  /**
   * Method description
   *
   *
   * @param item
   */
  public void remove(Course item)
  {
    SQLiteDatabase db = helper.getWritableDatabase();

    db.beginTransaction();

    try
    {
      String query = helper.getQuery(QUERY_REMOVE);

      AssertUtil.assertIsNotEmpty(query);

      SQLiteStatement statement = db.compileStatement(query);

      statement.bindString(1, item.getName());
      statement.execute();

      // TODO remove holes ??

      db.setTransactionSuccessful();
      fireEvent(item, DAOEvent.REMOVE);
    }
    finally
    {
      db.endTransaction();
    }
  }

  /**
   * Method description
   *
   *
   * @param item
   */
  public void update(Course item)
  {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  //~--- get methods ----------------------------------------------------------

  /**
   * Method description
   *
   *
   *
   * @param name
   *
   * @return
   */
  public Course get(String name)
  {
    Cursor c = helper.executeQuery(QUERY_SELECT, name);
    Course course = null;

    try
    {
      if (c.moveToNext())
      {
        course = new Course(name, new Date(c.getLong(0)));
      }
    }
    finally
    {
      c.close();
    }

    return course;
  }

  /**
   * Method description
   *
   *
   * @return
   */
  public List<Course> getAll()
  {
    Cursor c = helper.executeQuery(QUERY_SELECT_ALL);
    List<Course> courseList = new ArrayList<Course>();

    try
    {
      while (c.moveToNext())
      {
        courseList.add(new Course(c.getString(0), new Date(c.getLong(1))));
      }
    }
    finally
    {
      c.close();
    }

    return courseList;
  }

  //~--- fields ---------------------------------------------------------------

  /** Field description */
  private SQLiteHelper helper;
}
